package com.whq.thrift.api.web.base.serviceconf.db;

import org.apache.ibatis.jdbc.SqlBuilder;

import com.whq.common.StringUtil;

public class ModuleInstanceSqlProvider {
	public String getSelectRecordsSql(String moduleId) {
		SqlBuilder.BEGIN();
		SqlBuilder.SELECT("*");
		SqlBuilder.FROM("t_module_instance");

		if (StringUtil.isNotBlank(moduleId)) {
			SqlBuilder.WHERE("module_id like #{moduleId}");
		}

		SqlBuilder.ORDER_BY("module_id asc");
		return SqlBuilder.SQL() + " limit #{beginIndex}, #{maxCount} ";
	}
	
	public String getSelectCountSql(String moduleId) {
		SqlBuilder.BEGIN();
		SqlBuilder.SELECT("count(*)");
		SqlBuilder.FROM("t_module_instance");

		if (StringUtil.isNotBlank(moduleId)) {
			SqlBuilder.WHERE("module_id like #{moduleId}");
		}

		return SqlBuilder.SQL();
	}

	public String getUpdateSql(ModuleInstance moduleInstance) {
		SqlBuilder.BEGIN();
		SqlBuilder.UPDATE("t_module_instance");

		/* SqlBuilder.SET("instance_id = #{instanceId}"); */

		if (moduleInstance.getIp() != null) {
			SqlBuilder.SET("ip = #{ip}");
		}

		if (moduleInstance.getPort() >= 0) {
			SqlBuilder.SET("port = #{port}");
		}

		if (moduleInstance.getStatus() >= 0) {
			SqlBuilder.SET("status = #{status}");
		}

		if (moduleInstance.getUpdateTime() != null) {
			SqlBuilder.SET("update_time = #{updateTime}");
		}

		SqlBuilder.WHERE("module_id = #{moduleId}");
		return SqlBuilder.SQL();
	}
	
	public String getAllRecordsSql() {
		SqlBuilder.BEGIN();
		SqlBuilder.SELECT("*");
		SqlBuilder.FROM("t_module_instance");
		SqlBuilder.ORDER_BY("module_id asc");
		return SqlBuilder.SQL();
	}

}
